import Toast from 'vant/lib/toast';

const getLocalStorage = (...args) => {
  let storage = {};
  for (let arg of args) {
    storage[arg] = window.localStorage.getItem(arg) || "";
  }
  return storage;
};

const setLocalStorage = (data) => {
  for (let prop in data) {
    window.localStorage.setItem(prop, data[prop]);
  }
};

const removeLocalStorage = (...args) => {
  for (let arg of args) {
    window.localStorage.removeItem(arg);
  }
};

const debounce = (func, wait, immediate) => {
  let timeout, args, context, timestamp, result;
  return function () {
    context = this;
    args = arguments;
    timestamp = new Date();
    const later = () => {
      const last = (new Date()) - timestamp;
      if (last < wait) {
        timeout = setTimeout(later, wait - last);
      } else {
        timeout = null;
        result = func.apply(context, args);
      }
    };
    if (!timeout) {
      timeout = setTimeout(later, wait);
    }
    return result;
  };
};

/**
 * 对字符串进行空的默认设置
 * @param value
 * @param defaultValue
 * @returns {string}
 * @constructor
 */
const E = function (value, defaultValue = '') {
  return empty(value) ? defaultValue : value;
};


/**
 * 判断是否为空
 * @param value
 * @returns {boolean}
 */
const empty = function (value) {
  if (typeof (value) === 'undefined') return true;
  if (value === '') return true;
  if (value === null) return true;
  return false;
};

/**
 * toast 显示
 * @param msg
 * @param time
 */
const showToast = function (msg = '', time = 1000) {
  msg = E(msg, 'nothing');
  time = E(time, 1000);
  Toast({
    position: 'bottom',
    message: msg,
    duration: time
  });
};

/**
 * 是否微信浏览器
 * @returns {boolean}
 */
const isWeiXin = function () {
  let ua = navigator.userAgent.toLowerCase();
  if (ua.indexOf('micromessenger') !== -1) return true;
  return false;
};


const throttle = (func, wait, options) => {
  let timeout, context, args, result;
  let previous = 0;
  if (!options) options = {};

  let later = function () {
    previous = options.leading === false ? 0 : new Date();
    timeout = null;
    result = func.apply(context, args);
    if (!timeout) context = args = null; //显示地释放内存，防止内存泄漏
  };

  let throttled = function () {
    let now = new Date();
    if (!previous && options.leading === false) previous = now;
    let remaining = wait - (now - previous);
    context = this;
    args = arguments;
    if (remaining <= 0 || remaining > wait) {
      if (timeout) {
        clearTimeout(timeout);
        timeout = null;
      }
      previous = now;
      result = func.apply(context, args);
      if (!timeout) context = args = null;
    } else if (!timeout && options.trailing !== false) {
      timeout = setTimeout(later, remaining);
    }
    return result;
  };

  throttled.cancel = function () {
    clearTimeout(timeout);
    previous = 0;
    timeout = context = args = null;
  };

  return throttled;
};

const getLocationParam = function (name) {
  let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  let r = window.location.search.substr(1).match(reg);
  if (r != null) return decodeURIComponent(r[2]);
  return "";
};


export {
  getLocalStorage,
  setLocalStorage,
  removeLocalStorage,
  getLocationParam,
  debounce,
  throttle,
  E,
  empty,
  showToast,
  isWeiXin
};

export default {
  install(Vue) {
    Object.defineProperties(Vue.prototype, {
      $util: {
        value: {
          getLocalStorage,
          setLocalStorage,
          removeLocalStorage,
          getLocationParam,
          debounce,
          throttle,
          E,
          empty,
          showToast,
          isWeiXin
        }
      }
    });
  }
}
